var page = 1;
var pagenum = 10;
var fi = 1

var main = [
	[ //表头
		{
			field: 'company_name',
			title: '公司名称',
			sort: true,
			fixed: 'left'
		}, {
			field: 'account',
			title: '账号',
			sort: true
		}, {
			field: 'quota',
			title: '财政额度'
		}, {
			field: 'used_quota',
			title: '已用额度'
		}, {
			title: '额度操作',
			width: 250,
			toolbar: '#operation',
			fixed: 'right'
		}

	]
];
var data = {};
data.page = page;
data.pagenum = pagenum;
var where_json = {};
 {if $Think.get.is_check}
		where_json.is_check=['in','{$Think.get.is_check}']
	{/if}
	
	{if $Think.get.name}
		where_json.company_name=['like','%{$Think.get.name}%']
	{/if}
	{if $Think.get.phone}
		where_json.phone=['like','%{$Think.get.phone}%']
	{/if}
	{if $Think.get.company_name}
		where_json.company_name=['like','%{$Think.get.company_name}%']
	{/if}
data.where_json = JSON.stringify(where_json)

ajaxcompany(data)

function ajaxcompany(data) {
	console.log(data)
	Load()
	ajax(api.company.list, data, 'post').done(function(res) {
		Load(1)
		table.render({
			elem: '#company',
			cellMinWidth: 80,
			cols: main,
			data: res.data,
			limit: data.pagenum,
			even: true
		});
		if(fi == 1) {
			laypage.render({
				elem: 'page',
				count: res.count,
				layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
				theme: '#1E9FFF',
				jump: function(obj, first) {
					if(!first) {
						page = obj.curr
						data.page = page
						pagenum = obj.limit
						data.pagenum = pagenum
						ajaxcompany(data)
					} else {

					}
				}
			});
			fi = 2
		}
		table.on('tool(company)', function(obj) {
						var data1 = obj.data; //获得当前行数据
						var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
						var tr = obj.tr; //获得当前行 tr 的DOM对象
						if(layEvent === 'look') {
							var limiturl = "{:url('log')}?op=search&company_id=" + data1.company_id
								console.log(limiturl)
								layer.open({
									type: 2,
									title: '请选择',
									shadeClose: true,
									shade: 0.8,
									area: ['80%', '70%'],
									content: limiturl //iframe的url
								});
						}else if(layEvent === 'add'){
							layer.open({
								type: 1,
								id: "add",
								title: '增加额度’',
								content: "<div style='padding:10px'><input type='number' class='chang_main layui-input'/></div>",
								btn: ['确定', '取消'],
								btnAlign: 'c' //按钮居中
									,
								shade: 0 //不显示遮罩
									,
								yes: function() {
									Load()
									ajax(api.finance.quota,{
										add_or_delete:1,
										number:$('.chang_main').val(),
										company_id:data1.company_id
									},'post').done(function(res){
										Load(1)
											console.log(res)
											if(res.code == 1) {
												layer.closeAll();
												ajaxcompany(data)
											} else {
												Err('该公司无财政额度')
											}
									}).fail(function(){
										Load(1)
										Err('请求失败')
									})
								},
								btn2: function() {
									layer.closeAll();
								}
							});
						}else if(layEvent === 'reduce') {
							var limit = data1.quota;
							console.log(limit)
							layer.open({
								type: 1,
								id: "delect",
								title: '减少额度',
								content: "<div style='padding:10px'><input type='number' class='chang_main layui-input'/></div>",
								btn: ['确定', '取消'],
								btnAlign: 'c' //按钮居中
									,
								shade: 0 //不显示遮罩
									,
								yes: function() {
									Load()
									
									
									if(limit > 0) {
										if(limit>$('.chang_main').val()){
											ajax(api.finance.quota,{
												add_or_delete:2,
												number:$('.chang_main').val(),
												company_id:data1.company_id
											},'post').done(function(res){
												Load(1)
													console.log(res)
													if(res.code == 1) {
														layer.closeAll();
														ajaxcompany(data)
													} else {
														Err('该公司无财政额度')
													}
											}).fail(function(){
												Load(1)
												Err('请求失败')
											})
										}else{
											Err('额度不能小于0')
										}
									} else {
										Err('额度不能小于0')
									}
						
								},
								btn2: function() {
									layer.closeAll();
								}
							});
						}
				})
	}).fail(function() {
		Load(1)
		Err('请求失败')
	})
}

$('.limit').on('click', function() {
	

});

$('.addlimit').on('click', function() {
	var that = this

	
});
$('.reducelimit').on('click', function() {
	var that = this
	
});